package com.maikec.utils.mvc.controller;
/**
 * 邢帅教育
 * <p>
 * 本源代码由数据中心及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2018. [图片]xsteach.com all rights reserved.
 */

import com.maikec.configure.JwtConfig;
import com.maikec.utils.jwt.JwtFactory;
import com.maikec.utils.mybatis.CourseService;
import com.maikec.utils.mybatis.CourseServiceImpl;
import com.maikec.utils.mybatis.pojo.User;
import io.jsonwebtoken.*;
import io.jsonwebtoken.impl.DefaultClaims;
import io.jsonwebtoken.impl.compression.GzipCompressionCodec;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * @author maikec
 * @date 2018/7/19
 */
@Controller
@RequestMapping("iController")
@Slf4j
@Api("iControllerImpl 前端接口类")
public class IControllerImpl implements IController {

    private final CourseServiceImpl courseService;
    public IControllerImpl(CourseServiceImpl courseService) {
        this.courseService = courseService;
    }

    @GetMapping("user")
    @Override
    public ModelAndView selectById(@RequestParam(name = "id") int id) {
        ModelAndView modelAndView = new ModelAndView("user");
        User user = new User();
        user.setId(id);
        System.out.println(courseService.getUser(user).toString());
        System.out.println("id===="+courseService.getUser(id));
        modelAndView.addObject("user",courseService.selectById(user));
        return modelAndView;
    }
    @GetMapping("selectUserById/{id}")
    @Override
    public ModelAndView selectUserById(@PathVariable(name = "id") int id) {
        User user = new User();
        user.setId(id);
        ModelAndView modelAndView = new ModelAndView("user");
        modelAndView.addObject("user",courseService.selectById(user));
        return modelAndView;
    }

    @ApiOperation(value = "获取用户信息",notes = "通过id获取用户id，如果该id对应的用户不存在，则返回null")
    @ApiImplicitParam(name = "id", value = "用户Id", dataType = "integer",defaultValue = "3")
    @GetMapping("selectUser")
    @ResponseBody
    @Override
    public User selectUser(@RequestParam(name = "id") int id) {
        User user = new User();
        user.setId(id);
        return courseService.selectById(user);
    }
    @PostMapping("post")
    @ResponseBody
    @Override
    public User selectUserPost(@RequestBody User user) {
        return courseService.selectById(user);
    }

    @PostMapping("addUser")
    @ResponseBody
    @Override
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Map addUser(@RequestBody @Valid User user, BindingResult bindingResult) {
        Map<String, Object> result = new HashMap<>(16);
       if (bindingResult.hasErrors()) {
           result.put("code", 0);
           Set errorSet = new HashSet();
           for (ObjectError error : bindingResult.getAllErrors()) {
               errorSet.add(error.getDefaultMessage());
           }
           result.put("info", errorSet);
           result.put("message", "");
           return result;
       }

        result.put("code", courseService.insert(user));
        result.put("info", "");
        user.setId(3);
        courseService.selectById(user);
        return result;
    }
    @PostMapping("login4App")
    @ResponseBody
    @Override
    public String login4App(@RequestBody @Valid User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {

        }
        log.debug("用户登录");
       return JwtFactory.getInstance().generateToken("100");
    }
}
